/***************************************************
> Copyright (C) 2024 ==FLY== All rights reserved.
> File Name: file.c
> Author: fly
> Mail:2243920918@qq.com 
> Created Time: 2024年12月10日 星期二 10时16分50秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//cat函数实现
int mycat(const char *filename);

//cp函数实现
int mycp(const char *newfilename, const char *filename);

int main(int argc, const char* argv[])
{
    int num;
    num = mycat("cat.txt");
    if(num)
    {
        printf("文件读取成功！\n");
    }
    else
    {
        printf("文件读取失败！\n");
    }

    printf("\n");

    num = mycp(argv[1], argv[2]);
    if(num)
    {
         printf("文件复制成功！\n");
    }
    else
    {
        printf("文件复制失败！\n");
    }
    
    printf("%ld\n", sizeof(argv[3]));
    return 0;
}


//cat函数
int mycat(const char *filename)
{
    FILE *pf = NULL;
    char ch[1024] = {0};

    pf = fopen(filename, "r");
    if(pf == NULL)
    {
        printf("文件打开失败！");
        return 0;
    }
   
    while(fgets(ch, sizeof(ch), pf))
    {
        printf("%s", ch);
    }

    printf("\n");
    fclose(pf);

    return 1;
}

//cp函数
int mycp(const char *newfilename, const char *filename)
{
    FILE *rpf = NULL;
    FILE *wpf = NULL;
    char str[60] = {0};

    rpf = fopen(filename, "r");
    if(rpf == NULL)
    {
        printf("原文件打开失败！\n");
        return 0;
    }

    wpf = fopen(newfilename, "w");
    if(wpf == NULL)
    {
        fclose(rpf);
        printf("复制文件打开失败！\n");
        return 0;
    }

    while(fgets(str, sizeof(str), rpf))
    {
        fputs(str, wpf);
    }

    fclose(rpf);
    fclose(wpf);

    return 1;
}
